<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单分类" prop="categoryName">
        <el-input
          v-model="queryParams.categoryName"
          placeholder="请输入订单分类"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单信息" prop="attribute">
        <el-input
          v-model="queryParams.attribute"
          placeholder="请输入订单信息"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格属性" prop="tags">
        <el-input
          v-model="queryParams.tags"
          placeholder="请输入规格属性"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约时间">
        <el-date-picker
          v-model="daterangeOrderTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item> 
      
      <el-form-item label="联系人" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入联系人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="联系电话" prop="telephone">
        <el-input
          v-model="queryParams.telephone"
          placeholder="请输入联系人电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
     
      <el-form-item label="接单人" prop="receiveName">
        <el-input
          v-model="queryParams.receiveName"
          placeholder="请输入接单人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="接单电话" prop="receivePhone">
        <el-input
          v-model="queryParams.receivePhone"
          placeholder="请输入接单人电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
     
      <!-- <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="全部" value="" />
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item> -->
     
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-tabs  v-model="tabActiveName" @tab-click="handleTabClick">
      <el-tab-pane label="全部" name="all"></el-tab-pane>
      <el-tab-pane  v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :name="dict.dictValue"></el-tab-pane>
    </el-tabs>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="num" width="170" >
        <template slot-scope="scope">
          <el-button
              type="text"
              @click="handleShow(scope.row)"
            >{{scope.row.num}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="订单分类" align="center" prop="categoryName"  />
      <el-table-column label="订单信息" align="center" prop="attribute" width="200" show-overflow-tooltip />
      <el-table-column label="规格属性" align="center" prop="tags" show-overflow-tooltip />
      <el-table-column label="预约时间" align="center" prop="orderTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="区域" align="center" prop="district" width="170" >
        <template slot-scope="scope">
            <div>{{scope.row.orderAddress.area}}</div>
        </template>
      </el-table-column> -->
      <el-table-column label="服务地址" align="center" prop="address" width="200" show-overflow-tooltip  />
      <el-table-column label="联系人" align="center" prop="username" />
      <el-table-column label="联系电话" align="center" prop="telephone" />
      <el-table-column label="接单人" align="center" prop="receiveName" />
      <el-table-column label="接单电话" align="center" prop="receivePhone" />
      <!-- <el-table-column label="接单限制" align="center" prop="receiveLimit" /> -->
      <!-- <el-table-column label="所属机构" align="center" prop="company" /> -->
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="备注" align="center" prop="detail" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="应付" align="center" prop="payable" /> -->
      <!-- <el-table-column label="金额" align="center" prop="pay" /> -->
      <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleShow(scope.row)"
            >详情</el-button>
          <el-button
              size="mini"
              type="text" v-if="scope.row.status != 0 && scope.row.status != 30"
              icon="el-icon-search"
              @click="openDistributeReceive(scope.row)"
            >派单</el-button>
            <!-- <el-button
              size="mini"
              type="text" v-if="scope.row.status != 20 && scope.row.status != 0"
              icon="el-icon-circle-check"
              @click="finishOrder(scope.row)"
            >完成</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:order:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="服务信息" prop="attribute">
              <el-input v-model="form.attribute" placeholder="请选择服务" :readonly="true" >
                <el-button slot="append" icon="el-icon-search" @click="openSelectAttribute"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="订单信息" prop="detail">
              <el-input type="textarea" :rows="2" v-model="form.detail" placeholder="请输入订单信息" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="预约时间" prop="orderTime">
              <el-date-picker clearable size="small"
                v-model="form.orderTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择预约时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="选择地址" prop="address">
              <el-input v-model="form.address" placeholder="请选择地址" :readonly="true"  >
                <el-button slot="append" icon="el-icon-search" @click="openSelectAddress"></el-button>
              </el-input>
            </el-form-item>
          </el-col>          
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
             <el-form-item label="联系人" prop="username">
              <el-input v-model="form.username" placeholder="请输入联系人姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="telephone">
              <el-input v-model="form.telephone" placeholder="请输入联系人电话" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="接单人" prop="receiveName">
              <el-input v-model="form.receiveName" placeholder="请选择接单人" :readonly="true"  >
                <el-button slot="append" icon="el-icon-search" @click="openSelectReceive"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接单电话" prop="receivePhone">
              <el-input v-model="form.receivePhone" placeholder="请输入接单人电话" :readonly="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="所属公司" prop="company">
              <el-input v-model="form.company" placeholder="请输入接单人所属公司" />
            </el-form-item>        
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属代理" prop="proxy">
              <el-input v-model="form.proxy" placeholder="请输入接单人所属代理" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="应付" prop="payable">
              <el-input v-model="form.payable" placeholder="请输入应付" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实付" prop="pay">
              <el-input v-model="form.pay" placeholder="请输入实付" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" :rows="2" v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
      
        <select-attribute ref="selectAttrForm"  v-if="attrOpen" @finishSelected="finishSelectAttr"></select-attribute>
        <select-receive ref="selectReceiveForm"  v-if="receiveOpen" @finishSelected="finishSelectReceive"></select-receive>
        <select-address ref="selectAddressForm"  v-if="addressOpen" @finishSelected="finishSelectAddress"></select-address>        
      
    </el-dialog>

    <distribute-receive ref="distributeReceiveForm"  v-if="distributeOpen" @finishSelected="finishDistributeReceive"></distribute-receive>
    <finish-order ref="finishOrderForm" v-if="finishOpen" @finishSelected="finishOrderSelected" ></finish-order>
    <show-order ref="showOrderForm" v-if="showOpen" ></show-order>
  </div>
</template>

<script>
import { listProxyOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/business/order";
import SelectAttribute from '@/views/order/order/selectAttribute';
import SelectReceive from '@/views/order/order/selectReceive';
import SelectAddress from '@/views/order/order/selectAddress';
import DistributeReceive from '@/views/order/order/distributeReceive';
import FinishOrder from '@/views/order/order/finishOrder';
import ShowOrder from '@/views/order/order/showOrder.vue';

export default {
  name: "Order",
  components: {
    SelectAttribute,
    SelectReceive,
    SelectAddress,
    DistributeReceive,
    FinishOrder,
    ShowOrder
  },
  data() {
    return {
      tabActiveName: 'all',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单管理表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      attrOpen: false,
      receiveOpen: false,
      addressOpen: false,
      distributeOpen: false,
      finishOpen: false,
      showOpen: false,
      // 预约时间时间范围
      daterangeOrderTime: [],
      // 创建人名称时间范围
      daterangeCreateTime: [],
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        num: null,
        detail: null,
        orderTime: null,
        userId: null,
        username: null,
        telephone: null,
        receiveId: null,
        receiveName: null,
        receivePhone: null,
        receiveLimit: null,
        group: null,
        attribute: null,
        status: null,
        createName: null,
        createId: null,
        createTime: null,
        payable: null,
        pay: null,
        payMethod: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        attribute: [
          { required: true, message: "服务信息不能为空", trigger: "blur" }
        ],
        orderTime: [
          { required: true, message: "预约时间不能为空", trigger: "blur" }
        ],
        detail: [
          { required: true, message: "订单信息不能为空", trigger: "blur" }
        ],
        username: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        telephone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        receiveName: [
          { required: true, message: "接单人不能为空", trigger: "blur" }
        ],
        receivePhone: [
          { required: true, message: "接单电话不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("tb_order_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询订单管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeOrderTime && '' != this.daterangeOrderTime) {
        this.queryParams.params["beginOrderTime"] = this.daterangeOrderTime[0];
        this.queryParams.params["endOrderTime"] = this.daterangeOrderTime[1];
      }
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listProxyOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    queryList(status){
      this.queryParams.status = status;
      this.getList();
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    handleTabClick(tab, event){
      if (tab.name != 'all') {
        this.queryParams.status = tab.name;
      }else{
        this.queryParams.status = '';
      }
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        num: null,
        detail: null,
        orderTime: null,
        userId: null,
        username: null,
        telephone: null,
        receiveId: null,
        receiveName: null,
        receivePhone: null,
        receiveLimit: null,
        group: null,
        attribute: null,
        status: 0,
        remark: null,
        createName: null,
        createId: null,
        createTime: null,
        payable: null,
        pay: null,
        payMethod: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeOrderTime = [];
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单管理";
      });
    },
    openSelectAttribute(){
      this.attrOpen = true;
      this.$nextTick(()=>{
        this.$refs['selectAttrForm'].init();
      });
      
    },
    finishSelectAttr(data){
      this.form.attribute = data.name;
      this.form.pay = data.price;
      this.form.payable = data.price;
    },
    openSelectReceive(){
      this.receiveOpen = true;
      this.$nextTick(()=>{
        this.$refs['selectReceiveForm'].init();
      });
    },
    finishSelectReceive(data){
      console.log(data)
      this.form.receiveId = data.userId;
      this.form.receiveName = data.nickName;
      this.form.receivePhone = data.phonenumber;
      //
      this.form.proxyId = data.proxyId;
      this.form.proxy = data.proxy?.name;
      this.form.companyId = data.companyId;
      this.form.company = data.company?.name;
    },
    openSelectAddress(){
      this.addressOpen = true;
      this.$nextTick(()=>{
        this.$refs['selectAddressForm'].init();
      });
    },
    finishSelectAddress(data){
      console.log(data)
      this.form.orderAddress = data;
      this.form.address = data.detail;
      this.form.addressId = data.id;
      //
      this.form.userId = data.userId;
      this.form.username = data.userName;
      this.form.telephone = data.userTelephone;
    },
    handleShow(row){
      this.showOpen = true;
      this.$nextTick(() => {
        this.$refs['showOrderForm'].init(row);
      });
    },
   
    openDistributeReceive(row){
      this.distributeOpen = true;
      this.$nextTick(()=>{
        this.$refs['distributeReceiveForm'].init(row);
      });
    },
    finishDistributeReceive(data){
      this.getList();
    },
    finishOrder(row){
      this.finishOpen = true;
      this.$nextTick(()=>{
        this.$refs['finishOrderForm'].init(row);
      });
    },
    finishOrderSelected(data){
      this.getList();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.status = 10;//已接单
            addOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除订单管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrder(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
    }
  }
};
</script>
